Ext.onReady(function() {
	Ext.QuickTips.init(); // 如要实现错误提示，需要此初始化
	Ext.form.Field.prototype.msgTarget = 'side';

	var height = this.document.documentElement.clientHeight;
	var width = this.document.documentElement.clientWidth;

	// top区域开始

	// top区域结束

	// 员工区域开始
	var employeesm = new Ext.grid.CheckboxSelectionModel();

	var employeecm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),
			employeesm, {
				header : "员工编号",
				dataIndex : "EmployeeID",
				sortable : true
			}, {
				header : "员工姓名",
				dataIndex : "Name",
				sortable : true
			}, {
				header : "员工联系方式",
				dataIndex : "Contact",
				sortable : true
			}, {
				header : "员工密码",
				dataIndex : "Password",
				sortable : true
			}]);

	var aaProxy = new Ext.data.HttpProxy({
				url : "employee",
				method : "GET"
			});

	var employeestore = new Ext.data.Store({
				proxy : aaProxy,
				reader : new Ext.data.JsonReader({})
			});
    employeestore.load();
	var employeepp = new Ext.grid.GridPanel({
		id : 'employeepp',
		title : '员工信息',
		region : 'center',
		autoShow : true,
		height : height,
		width : width,

		split : true,
		minSize : 175,
		maxSize : 400,
		frame : true,
		columnLines : true,
		sm : employeesm,
		store : employeestore,
		cm : employeecm,
		tbar : [{
			pressed : false,
			text : '添加员工',
			icon : "static/images/add.gif",
			handler : function() {
				var TEmployeeID = new Ext.form.Field({
							id : 'EmployeeID',
							name : 'EmployeeID',
							allowBlank : true,
							fieldLabel : "员工编号"
						});
				var TEmployeeName = new Ext.form.Field({
							id : 'EmployeeName',
							name : 'EmployeeName',
							allowBlank : true,
							fieldLabel : "员工姓名"
						});
				var TContact = new Ext.form.Field({
							id : 'Contact',
							name : 'Contact',
							allowBlank : true,
							fieldLabel : "联系方式"
						});
				var formp = new Ext.form.FormPanel({
					title : "员工添加",
					closable : true,
					labelAlign : "right",
					buttonAlign : "center",
					// width : 350,
					// height : 500,
					autoHeight : true,
					autoWidth : true,
					frame : true,
					split : true,
					items : [TEmployeeID, TEmployeeName, TContact],
					url : "employee?method=add",
					method : "post",
					buttons : [{
						text : "确定",
						icon : "extjsLib/resources/static/images/default/dd/disk.png",
						handler : function() {
							formp.getForm().submit({
								success : function(form, action) {
									var msg = action.result.msg;
									if (msg == "yes") {
										employeestore.reload();
										Ext.Msg.alert("成功", "恭喜，员工信息添加成功！");
										addwin.close();
									}
									if (msg == "no") {
										Ext.Msg.alert("失败", "员工号重复或不存在！");
									}
									// store.reload();
								},
								failure : function(response) {
									Ext.Msg.alert("失败", "对不起，员工信息添加异常！");
								}
							});
						}
					}, {
						text : "重置",
						icon : "extjsLib/resources/static/images/default/dd/delete.gif",
						handler : function() {
							formp.getForm().reset();
						}
					}]
				});
				var addwin = new Ext.Window({
							title : "",
							width : 350,
							// height : 350,
							closeAction : "close",
							closeable : true,
							items : formp,
							collapsible : true,
							autoHeight : true,
							modal : true
						});
				addwin.show();
			}
		}, {
			xtype : "tbseparator"
		}, {
			pressed : false,
			text : '员工删除',
			icon : "static/images/cross.gif",
			handler : function() {
				var selModel = employeepp.getSelectionModel();
				if (selModel.hasSelection()) {
					Ext.Msg.confirm("确认", "您确定要删除选择的记录吗？", function(btn) {
						if (btn == "yes") {
							var records = selModel.getSelections();
							var ids = [];
							for (var i = 0; i < records.length; i++) {
								ids.push(records[i].get("EmployeeID")); // 获取选择的条目id
							}
							Ext.Ajax.request({
										url : "employee?method=delete",
										params : {
											ids : ids
										},
										method : "post",
										success : function(form, action) {
											Ext.MessageBox.alert("结果", "删除成功！");
											employeestore.reload();
										},
										failure : function() {
											var messagestr = response.result.msg;
											alert(messagestr);
											employeestore.reload();
											// Ext.Msg
											// .alert(
											// "结果",
											// "对不起，删除异常！");
										}
									});

						}
					});
				} else {
					Ext.Msg.alert("错误", "请选择要删除的行！");
				}
			}
		}, {
			xtype : "tbseparator"
		}, {
			pressed : false,
			text : '修改员工信息',
			icon : "static/images/details.gif",
			handler : function() {
				// Ext.Msg.alert("修改");
				var selModel = employeepp.getSelectionModel();
				var record;
				if (!selModel.hasSelection()) {
					Ext.Msg.alert("错误", "请选择要修改的行！");
				} else if (selModel.getSelections().length > 1) {
					Ext.Msg.alert("错误", "一次只能选择一行！");
				} else {
					record = selModel.getSelected();

					var TEmployeeID = new Ext.form.Field({
								id : 'EmployeeID',
								name : 'EmployeeID',
								allowBlank : true,
								fieldLabel : "员工编号"
							});
					var TEmployeeName = new Ext.form.Field({
								id : 'EmployeeName',
								name : 'EmployeeName',
								allowBlank : true,
								fieldLabel : "员工姓名"
							});
					var TContact = new Ext.form.Field({
								id : 'Contact',
								name : 'Contact',
								allowBlank : true,
								fieldLabel : "联系方式"
							});

					var form = new Ext.form.FormPanel({
						width : 350,
						labelAlign : "right",
						frame : true,
						items : [TEmployeeID, TEmployeeName, TContact],
						url : "employee?method=edit",
						buttonAlign : "center",
						method : "post",
						buttons : [{
							text : "员工修改",
							icon : "extjsLib/resources/static/images/default/dd/disk.png",
							handler : function() {
								var json = {
									success : function(form, action){
										var msg=action.result.msg;
										if (msg == "yes") {
											Ext.Msg.alert("成功", "信息修改成功！");
											win.close();
											employeestore.reload();
										} else {
											Ext.Msg.alert("失败", "信息修改异常！");
										}
									}
								};
								form.getForm().submit(json);
								employeestore.reload();
							}
								// win.close();
						}, {
							text : "关闭",
							icon : "extjsLib/resources/static/images/default/dd/delete.gif",
							handler : function() {
								win.close();
							}
						}],
						bbar : [new Ext.PagingToolbar({
							store : employeestore,
							displayInfo : true,
							pageSize : 11,
							beforePageText : "当前第",
							afterPageText : "页，共{0}页",
							lastText : "尾页",
							nextText : "下一页",
							prevText : "上一页",
							firstText : "首页",
							refreshText : "刷新页面",
							displayInfo : true,
							displayMsg : "当前显示 {0} - {1}条, 共 {2}",
							emptyMsg : "没有记录"
						})]

					});
					var win = new Ext.Window({
								title : "员工修改",
								id : "edit",
								// width : 500,
								width : 350,
								modal : true,
								autoHeight : true,
								items : [form]
							});
					win.show();

					// set the form default values
					form.getForm().setValues({
								"EmployeeID" : record.get("EmployeeID"),
								"EmployeeName" : record.get("EmployeeName"),
								"Contact" : record.get("Contact")
							});
				}
			}
		}, {
			xtype : "tbseparator"
		}, {
			pressed : false,
			text : '所有员工',
			icon : "static/images/table_refresh.png",
			handler : function() {
				employeestore.proxy = aaProxy;
				employeestore.load({
							params : {
								start : 0,
								limit : 20
							}
						});
			}
		}],
		bbar : [new Ext.PagingToolbar({
			store : employeestore,
			displayInfo : true,
			pageSize : 11,
			beforePageText : "当前第",
			afterPageText : "页，共{0}页",
			lastText : "尾页",
			nextText : "下一页",
			prevText : "上一页",
			firstText : "首页",
			refreshText : "刷新页面",
			displayInfo : true,
			displayMsg : "当前显示 {0} - {1}条, 共 {2}",
			emptyMsg : "没有记录"
		})]
	});
	var win = new Ext.Panel({
				layout : 'border',
				height : height,
				width : width,
				items : [employeepp]
			});
	win.render(Ext.getBody());
});